package org.blackcat.service.impl;

import org.blackcat.common.DESUtil;
import org.blackcat.common.DateUtils;
import org.blackcat.core.annotation.Validation;
import org.blackcat.common.UUIDUtils;
import org.blackcat.dao.UserDao;
import org.blackcat.dao.redis.RedisHandle;
import org.blackcat.domain.common.MsgResponse;
import org.blackcat.domain.enums.ResponseCodeEnum;
import org.blackcat.domain.User;
import org.blackcat.domain.enums.UserEnum;
import org.blackcat.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by Duo Duo on 2016/12/26.
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RedisHandle redisHandle;

    @Override
    public MsgResponse<Object> existsAccount(String account) {
        int rs = userDao.existsAccount(account);
        if(rs > 0){
            return new MsgResponse<Object>(ResponseCodeEnum.REQUEST_PARAMETER_ERROR,"账号已存在");
        }
        return new MsgResponse<Object>(ResponseCodeEnum.SUCCESS);
    }
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    @Validation
    public MsgResponse<User> create(User user) throws Exception {
        int rs = userDao.existsAccount(user.getAccount());
        if(rs > 0){
            return new MsgResponse<User>(ResponseCodeEnum.REQUEST_PARAMETER_ERROR,"账号已存在");
        }
        user.setId(UUIDUtils.uuid());
        user.setGmtCreate(new Date());
        user.setGmtModified(new Date());
        int rs2 = userDao.create(user);
        if(rs2 > 0){
            String token = UUIDUtils.uuid();
            String userIdAES = DESUtil.aesDecrypt(user.getId(),UserEnum.USER_AES_KEY.getValue());
            redisHandle.setString(token,userIdAES,2*60*60);
            user.setToken(token);
            return new MsgResponse<User>(ResponseCodeEnum.SUCCESS,user);
        }
        return new MsgResponse<User>(ResponseCodeEnum.EXCEPTION,"服务器出现错误");
    }

    @Override
    @Validation
    public MsgResponse<User> findByAccount(String account,String password) throws Exception {
        User user = userDao.findByAccount(account);
        if(user == null){
            return new MsgResponse<User>(ResponseCodeEnum.REQUEST_PARAMETER_ERROR,"账号不存在");
        }else if(!user.getPassword().equals(password)){
            return new MsgResponse<User>(ResponseCodeEnum.REQUEST_PARAMETER_ERROR,"密码错误");
        }
        String token = UUIDUtils.uuid();
        redisHandle.setString(token, DESUtil.aesEncrypt(user.getId(), UserEnum.USER_AES_KEY.getValue()),2*60*60);
        user.setToken(token);
        return new MsgResponse<User>(ResponseCodeEnum.SUCCESS,user);
    }

    @Override
    public MsgResponse<Object> update(User user,String token) throws Exception {
        String userIdAES = redisHandle.getString(token);
        String userId = DESUtil.aesDecrypt(userIdAES,UserEnum.USER_AES_KEY.getValue());
        user.setGmtModified(new Date());
        user.setId(userId);
        int rs = userDao.update(user);
        if(rs > 0){
            return new MsgResponse<Object>(ResponseCodeEnum.SUCCESS);
        }
        return new MsgResponse<Object>(ResponseCodeEnum.EXCEPTION);
    }
}
